#!/usr/local/bin/perl -w
use strict;
use utf8;
use lib '../lib';

use NG::Gen::App qw(
NG::Gen::AuthCgi
);

use JSON::XS;
use NG::Gen::Users::Constants;
my $app = new NG::Sys::App();

$app->run;

sub on_before_process {
	my ($app,$form,$obj) = @_;
	unless($form->{user}){
		$form->set_error(403=>"not atorized");
		$form->process_as_json;
		return 0;
	}
	return 1;
}
sub cmd_default {
	my ($app,$form,$obj) = @_;
	$form->set_error(400=>"bad request");
	$form->process_as_json;
}
sub cmd_chat_save {
	my ($app,$form,$obj) = @_;
	my $cur = $form->{user}->setting->chat;
	my $val;
	if($val = $form->{input}{clock}){
		$cur->{clock} = 1;
	}else{
		$cur->{clock} = undef;
	}
	if($val = $form->{input}{trans}){
		$cur->{trans} = 1;
	}else{
		$cur->{trans} = undef;
	}
	if($val = $form->{input}{smiles}){
		$cur->{smiles} = 1;
	}else{
		$cur->{smiles} = undef;
	}
	if($val = $form->{input}{timing}){
		$cur->{timing} = 1;
	}else{
		$cur->{timing} = undef;
	}
	if($val = $form->{input}{animate}){
		$cur->{animate} = 1;
	}else{
		$cur->{animate} = undef;
	}
	if($val = $form->{input}{max_str} and $val =~ /^\d+$/){
		if(100 > $val){
			$val = 100;
		}elsif($val > 1000){
			$val = 1000;
		}
		$cur->{max_str} = $val;
	}else{
		$cur->{max_str} = 500;
	}
	$form->{user}->setting->chat($cur);
	$form->{data} = $cur;
	$form->process_as_json;
}
